package com.sanshi.project.assets.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.sanshi.project.admin.api.entity.ContractEntity;
import com.sanshi.project.admin.api.entity.ContractHouseEntity;
import com.sanshi.project.admin.api.entity.ContractLesseeEntity;
import com.sanshi.project.admin.api.entity.HouseEntity;
import com.sanshi.project.assets.mapper.ContractHouseMapper;
import com.sanshi.project.assets.mapper.ContractLesseeMapper;
import com.sanshi.project.assets.mapper.ContractMapper;
import com.sanshi.project.assets.mapper.HouseMapper;
import com.sanshi.project.assets.service.ContractService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 租赁合同
 *
 * @author nikon
 * @date 2023-11-27 08:46:34
 */
@Service
@RequiredArgsConstructor
public class ContractServiceImpl extends ServiceImpl<ContractMapper, ContractEntity> implements ContractService {
    private final HouseMapper houseMapper;
    private final ContractHouseMapper contractHouseMapper;
    private final ContractLesseeMapper contractLesseeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveDeep(ContractEntity contract) {
        baseMapper.insert(contract);
        // 合同房屋资产
        for (ContractHouseEntity contractHouse : contract.getContractHouseList()) {
            contractHouse.setContractId(contract.getId());
            contractHouseMapper.insert(contractHouse);
            // 获取房屋
            HouseEntity house = houseMapper.selectById(contractHouse.getId());
            // 房屋状态出租（“50”）
            house.setStatus("50");
            houseMapper.updateById(house);
        }
        // 合同承租户
        for (ContractLesseeEntity contractLessee : contract.getContractLesseeList()) {
            contractLessee.setContractId(contract.getId());
            contractLesseeMapper.insert(contractLessee);
        }
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDeep(ContractEntity contract) {
        baseMapper.updateById(contract);
        // 查询所有房屋列表
        MPJLambdaWrapper<HouseEntity> wrapper = new MPJLambdaWrapper<HouseEntity>()
                .selectAll(HouseEntity.class)
                .leftJoin(ContractHouseEntity.class, ContractHouseEntity::getId, HouseEntity::getId)
                .leftJoin(ContractEntity.class, ContractEntity::getId, ContractHouseEntity::getContractId)
                .eq(ContractEntity::getId, contract.getId());
        // 联表查询
        List<HouseEntity> houseList = houseMapper.selectJoinList(HouseEntity.class, wrapper);
        // 合同房屋资产
        for (HouseEntity house : houseList) {
            // 房屋状态空置（“1”）
            house.setStatus("1");
            houseMapper.updateById(house);
        }
        // 删除所有合同房屋和租户
        contractHouseMapper.delete(Wrappers.<ContractHouseEntity>lambdaQuery().eq(ContractHouseEntity::getContractId, contract.getId()));
        contractLesseeMapper.delete(Wrappers.<ContractLesseeEntity>lambdaQuery().eq(ContractLesseeEntity::getContractId, contract.getId()));
        // 合同房屋资产
        for (ContractHouseEntity contractHouse : contract.getContractHouseList()) {
            contractHouse.setContractId(contract.getId());
            contractHouseMapper.insert(contractHouse);
            // 获取房屋
            HouseEntity house = houseMapper.selectById(contractHouse.getId());
            // 房屋状态出租（“50”）
            house.setStatus("50");
            houseMapper.updateById(house);
        }
        // 合同承租户
        for (ContractLesseeEntity contractLessee : contract.getContractLesseeList()) {
            contractLessee.setContractId(contract.getId());
            contractLesseeMapper.insert(contractLessee);
        }
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeDeep(Long[] ids) {
        baseMapper.deleteBatchIds(CollUtil.toList(ids));
        contractHouseMapper.delete(Wrappers.<ContractHouseEntity>lambdaQuery().in(ContractHouseEntity::getContractId, ids));
        contractLesseeMapper.delete(Wrappers.<ContractLesseeEntity>lambdaQuery().in(ContractLesseeEntity::getContractId, ids));
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeChild(Long[] ids) {
        contractHouseMapper.deleteBatchIds(CollUtil.toList(ids));
        contractLesseeMapper.deleteBatchIds(CollUtil.toList(ids));
        return Boolean.TRUE;
    }
}